package com.sxbang.friday.service.impl;

import com.sxbang.friday.base.result.Results;
import com.sxbang.friday.dao.SysRoleUserMapper;
import com.sxbang.friday.dao.UserDao;
import com.sxbang.friday.model.SysRoleUser;
import com.sxbang.friday.model.SysUser;
import com.sxbang.friday.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;
	@Autowired
	private SysRoleUserMapper sysRoleUserMapper;

	@Override
	public SysUser getUser(String username) {
		return userDao.getUser(username);
	}

	@Override
	public Results<SysUser> getAllUsersByPage(Integer offset, Integer limit) {
		return Results.success(userDao.countAllUsers().intValue(),userDao.getAllUsersByPage(offset,limit));
	}

	@Override
	public Results save(SysUser sysUser, Integer roleId) {
		//日过角色id不为空
		if (roleId != null){
			//保存user到数据库
			userDao.save(sysUser);
			//把id保存到userrole数据表
			SysRoleUser sysRoleUser = new SysRoleUser();
			//给角色id赋值
			sysRoleUser.setRoleId(roleId);
			//给用户id赋值，MyBatis+MySQL 返回插入后会的主键的ID
			sysRoleUser.setUserId(sysUser.getId().intValue());
			sysRoleUserMapper.save(sysRoleUser);
			//成功返回
			return Results.success();
		}
		return Results.failure();
	}
}
